﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RPGProject.GameWorld;
using RPGProject.IOBuffers;
namespace RPGProject.LoopSystem
{
    /// <summary>
    /// A class for taking care of all the text based screens.
    /// </summary>
    class Visuals : Loop
    {
        /// <summary>
        /// The current type of screen to render.
        /// </summary>
        private VisualType vType;
        /// <summary>
        /// Property for the above.
        /// </summary>
        public VisualType ToShow
        {
            get { return this.vType; }
            set { this.vType = value;}
        }
        /// <summary>
        /// World to grab log screen from.
        /// </summary>
        private World world;
        // Private variables for the various screens.
        private Screen openingScreen;
        private Screen helpScreen;
        private Screen loseScreen;
        private Screen winScreen;
        /// <summary>
        /// Constructor for a basic visuals.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="w"></param>
        /// <param name="lm"></param>
        public Visuals(int width, int height, World w, LoopManager lm) : base(lm, 0)
        {
            this.world = w;
            this.openingScreen = null;
            try
            {
                this.openingScreen = new Screen(width, height);
                this.openingScreen.AllocateScreen(0, 0, LogOutputBuffer.CreateLogFromFile("opening.txt", width, height));
            }
            catch (Exception e)
            {
                Console.WriteLine("opening.txt failed to load.  Please recreate file.");
                Console.ReadKey();
                return;
            }
            this.helpScreen = null;
            try
            {

                this.helpScreen = new Screen(width, height);
                this.helpScreen.AllocateScreen(0, 0, LogOutputBuffer.CreateLogFromFile("help.txt", width, height));
            }
            catch (Exception e)
            {
                Console.WriteLine("help.txt failed to load.  Please recreate file.");
                Console.ReadKey();
                return;
            }
            this.winScreen = null;
            try
            {
                this.winScreen = new Screen(width, height);
                this.winScreen.AllocateScreen(0, 0, LogOutputBuffer.CreateLogFromFile("win.txt", width, height));
            }
            catch (Exception e)
            {
                Console.WriteLine("win.txt failed to load.  Please recreate file.");
                Console.ReadKey();
            }
            this.loseScreen = null;
            try
            {
                this.loseScreen = new Screen(width, height);
                this.loseScreen.AllocateScreen(0, 0, LogOutputBuffer.CreateLogFromFile("lose.txt", width, height));
            }
            catch (Exception e)
            {
                Console.WriteLine("lose.txt failed to load.  Please recreate file.");
                Console.ReadKey();
            }
        }
        /// <summary>
        /// Shows the screen and waits for thes Space Bar to be pressed.
        /// </summary>
        /// <returns></returns>
        public override FuncType RunOnce()
        {
            switch (this.vType)
            {
                case VisualType.HELP:
                    this.world.CurrentScreen = this.helpScreen;
                    break;
                case VisualType.LOSE:
                    this.world.CurrentScreen = this.loseScreen;
                    break;
                case VisualType.WIN:
                    this.world.CurrentScreen = this.winScreen;
                    break;
                case VisualType.LOG:
                    this.world.CurrentScreen = this.world.LogScreen;
                    break;
                case VisualType.OPEN:
                    this.world.CurrentScreen = this.openingScreen;
                    break;
            }
            if (this.loopManager.KeyState.GetPressedKeys().Contains(Microsoft.Xna.Framework.Input.Keys.Space))
            {
                if(this.vType != VisualType.LOSE || this.vType != VisualType.WIN)
                    this.world.ResetScreen();
                return FuncType.REMOVE_THIS;
            }
            else
                return FuncType.REPEAT;
        }
    }
    /// <summary>
    /// Enum detailing the different types of screens to be shown.
    /// </summary>
    public enum VisualType
    {
        WIN,
        LOSE,
        OPEN,
        LOG,
        HELP
    }
}
